package org.glut.AppStoreProject.service;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.glut.AppStoreProject.entity.App;
import org.glut.AppStoreProject.mapper.AppMapper;
import org.glut.AppStoreProject.utils.MybatisUtil;
import org.glut.AppStoreProject.vo.SearchTagAppVO;

import java.util.List;

/**
 * @author: gluglu2
 * @date: 2025/7/14 11:23
 */
public class AppServiceImpl {
    static Logger logger = Logger.getLogger(AppServiceImpl.class);

    /**
     * @description: 根据id查询app信息
     * @param app
     * @return org.glut.AppStoreProject.entity.App
     * @author: gluglu2
     * @date: 2025/7/14 11:34
     */
    public App getAppById(App app) {
        try(
                SqlSession sqlSession = MybatisUtil.getSession()
        ) {
            AppMapper mapper = sqlSession.getMapper(AppMapper.class);
            return mapper.getApp(app);
        } catch(Exception e) {
            logger.error("查询app信息失败",e);
        }
        return null;
    }

    /**
     * @description: 根据name查询app信息
     * @param app
     * @return org.glut.AppStoreProject.entity.App
     * @author: gluglu2
     * @date: 2025/7/14 11:34
     */
    public App getAppByName(App app) {
        try(
                SqlSession sqlSession = MybatisUtil.getSession()
        ) {
            AppMapper mapper = sqlSession.getMapper(AppMapper.class);
            return mapper.getApp(app);
        } catch(Exception e) {
            logger.error("查询app信息失败",e);
        }
        return null;
    }

    /***
     * @description: 根据app-type查询app列表信息
     * @param app
     * @return java.util.List<org.glut.AppStoreProject.entity.App>
     * @author: gluglu2
     * @date: 2025/7/15 15:36
     */
    public List<App> getAppsByType(App app) {
        try(
                SqlSession sqlSession = MybatisUtil.getSession()
        ) {
            AppMapper mapper = sqlSession.getMapper(AppMapper.class);
            return mapper.getApps(app);
        } catch(Exception e) {
            logger.error("查询app列表信息失败",e);
        }
        return null;
    }

    public SearchTagAppVO getTop5(String tag,Integer type) {
        try(
             SqlSession sqlSession = MybatisUtil.getSession()
        ) {
            AppMapper mapper = sqlSession.getMapper(AppMapper.class);
            SearchTagAppVO searchTagAppVO = new SearchTagAppVO();
            searchTagAppVO.setTagName(tag);
            searchTagAppVO.setAppList(mapper.getTop5(type));
            return searchTagAppVO;
        } catch(Exception e) {
            logger.error("查询app信息失败",e);
        }
        return null;
    }

    public SearchTagAppVO getRank10(String tag) {
        try(
                SqlSession sqlSession = MybatisUtil.getSession()
        ) {
            AppMapper mapper = sqlSession.getMapper(AppMapper.class);
            SearchTagAppVO searchTagAppVO = new SearchTagAppVO();
            searchTagAppVO.setTagName(tag);
            searchTagAppVO.setAppList(mapper.getRank10());
            return searchTagAppVO;
        } catch(Exception e) {
            logger.error("查询app信息失败",e);
        }
        return null;
    }
//首页应用
    public List<App> getAppList() {
        try (SqlSession session = MybatisUtil.getSession()) {
            AppMapper mapper = session.getMapper(AppMapper.class);

            return mapper.selectAllApps();
        }
    }
}
